package com.cskaoyan.service;


import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.goods.wxbean.CategoryVO;
import com.cskaoyan.bean.market.*;
import com.cskaoyan.bean.wxcategory.CategoryIndexVO;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.mapper.ConfigMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    ConfigMapper configMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public CategoryList list() {
        List<Category> categories = categoryMapper.selectByExample(null);
        CategoryExample categoryExample = new CategoryExample();
        String level = categories.get(0).getLevel();
        categoryExample.createCriteria().andLevelEqualTo(level);
        List<Category> categories1 = categoryMapper.selectByExample(categoryExample);
        PageInfo<Category> categoryPageInfo = new PageInfo<>(categories1);
        long total = categoryPageInfo.getTotal();
        int pages = categoryPageInfo.getPages();
        int page = categoryPageInfo.getPageNum();
        ArrayList<CategoryListBean> categoryListBeans = new ArrayList<>();
        for (Category category : categories1) {
            if(category.getDeleted() == false){
                CategoryListBean categoryListBean = new CategoryListBean();
                categoryListBean.setId(category.getId());
                categoryListBean.setName(category.getName());
                categoryListBean.setKeywords(category.getKeywords());
                categoryListBean.setDesc(category.getDesc());
                categoryListBean.setIconUrl(category.getIconUrl());
                categoryListBean.setPicUrl(category.getPicUrl());
                categoryListBean.setLevel(category.getLevel());
                CategoryExample categoryExample1 = new CategoryExample();
                categoryExample1.createCriteria().andPidEqualTo(category.getId());
                ArrayList<CategoryChildrenBean> categoryChildrenBeans = new ArrayList<>();
                List<Category> categories2 = categoryMapper.selectByExample(categoryExample1);
                for (Category category1 : categories2) {
                    if(category1.getDeleted() == false){
                        CategoryChildrenBean categoryChildrenBean = new CategoryChildrenBean();
                        categoryChildrenBean.setId(category1.getId());
                        categoryChildrenBean.setName(category1.getName());
                        categoryChildrenBean.setKeywords(category1.getKeywords());
                        categoryChildrenBean.setIconUrl(category1.getIconUrl());
                        categoryChildrenBean.setPicUrl(category1.getPicUrl());
                        categoryChildrenBean.setLevel(category1.getLevel());
                        categoryChildrenBeans.add(categoryChildrenBean);
                    }
                }
                categoryListBean.setChildren(categoryChildrenBeans);
                categoryListBeans.add(categoryListBean);
            }
        }
        CategoryList categoryList = new CategoryList();
        categoryList.setLimit((int) total);
        categoryList.setTotal((int) total);
        categoryList.setList(categoryListBeans);
        categoryList.setPage(page);
        categoryList.setPages(pages);
        return categoryList;
    }


    @Override
    public void delete(CategoryDeleteBo categoryDeleteBo) {
        if (categoryDeleteBo.getChildren() != null) {
            //一级类目
            int pid = categoryDeleteBo.getId();
            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andPidEqualTo(pid);
            Date updateTime = new Date();
            List<Category> categories = categoryMapper.selectByExample(categoryExample);
            for (Category category : categories) {
                updateDeleteId(updateTime, category.getId(), category.getName(), category.getKeywords(),
                        category.getDesc(), category.getIconUrl(), category.getPicUrl(), category.getLevel());
            }
            updateDeleteId(updateTime, categoryDeleteBo.getId(), categoryDeleteBo.getName(), categoryDeleteBo.getKeywords(),
                    categoryDeleteBo.getDesc(), categoryDeleteBo.getIconUrl(), categoryDeleteBo.getPicUrl(), categoryDeleteBo.getLevel());
        } else {
            //二级类目
            updateDeleteId(new Date(), categoryDeleteBo.getId(), categoryDeleteBo.getName(), categoryDeleteBo.getKeywords(),
                    categoryDeleteBo.getDesc(), categoryDeleteBo.getIconUrl(), categoryDeleteBo.getPicUrl(), categoryDeleteBo.getLevel());
        }
    }

    private void updateDeleteId(Date updateTime, int id, String name, String keywords, String desc, String iconUrl, String picUrl, String level) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andIdEqualTo(id);
        Category category1 = new Category();
        category1.setId(id);
        category1.setName(name);
        category1.setKeywords(keywords);
        category1.setDesc(desc);
        category1.setIconUrl(iconUrl);
        category1.setPicUrl(picUrl);
        category1.setLevel(level);
        category1.setUpdateTime(updateTime);
        category1.setDeleted(true);
        categoryMapper.updateByExampleSelective(category1,categoryExample);
    }

    @Override
    public CateGoryLiVo l1() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        PageInfo<Category> categoryPageInfo = new PageInfo<>(categories);
        long total = categoryPageInfo.getTotal();
        int pages = categoryPageInfo.getPages();
        int page = categoryPageInfo.getPageNum();
        ArrayList<CategoryLiListBean> categoryLiListBeans = new ArrayList<>();
        for (Category category : categories) {
            CategoryLiListBean categoryLiListBean = new CategoryLiListBean();
            categoryLiListBean.setLabel(category.getName());
            categoryLiListBean.setValue(category.getId());
            categoryLiListBeans.add(categoryLiListBean);
        }
        CateGoryLiVo cateGoryLiVo = new CateGoryLiVo();
        cateGoryLiVo.setTotal((int) total);
        cateGoryLiVo.setLimit((int) total);
        cateGoryLiVo.setList(categoryLiListBeans);
        cateGoryLiVo.setPage(page);
        cateGoryLiVo.setPages(pages);
        return cateGoryLiVo;
    }

    @Override
    public void update(CategoryUpdate categoryUpdate) {
        //level 2→1、2    1→1 不用考虑子类目的问题
        //level 1→2 需要考虑子类目的问题，策略：直接让子类也移至同样的新的父类目下面
        String level = categoryUpdate.getLevel();
        List<CategoryChildrenBean> children = categoryUpdate.getChildren();
        if(children == null){
            //2→1、2
            if("L1".equals(level)){
                //2-1 更改pid为0
                updateCategory2(categoryUpdate.getId(), 0, categoryUpdate.getKeywords(), categoryUpdate.getDesc(), categoryUpdate.getIconUrl(), categoryUpdate.getPicUrl(), categoryUpdate.getLevel());
            }else{
                //2-2 不用更改pid
                updateCategory(categoryUpdate);
            }
        }else if("L1".equals(level)){
            //1→1 不用更改pid
            updateCategory(categoryUpdate);

        }else{
            //1→2 需要考虑子类目的问题，策略：直接让子类也移至同样的新的父类目下面
            for (CategoryChildrenBean child : children) {
                updateCategory2(child.getId(), categoryUpdate.getPid(), child.getKeywords(), child.getDesc(), child.getIconUrl(), child.getPicUrl(), child.getLevel());
            }
            updateCategory2(categoryUpdate.getId(), categoryUpdate.getPid(), categoryUpdate.getKeywords(), categoryUpdate.getDesc(), categoryUpdate.getIconUrl(), categoryUpdate.getPicUrl(), categoryUpdate.getLevel());
        }
    }

    private void updateCategory2(int id, int pid, String keywords, String desc, String iconUrl, String picUrl, String level2) {
        Category category = new Category();
        category.setId(id);
        category.setPid(pid);
        category.setKeywords(keywords);
        category.setDesc(desc);
        category.setIconUrl(iconUrl);
        category.setPicUrl(picUrl);
        category.setLevel(level2);
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    private void updateCategory(CategoryUpdate categoryUpdate) {
        Category category = new Category();
        category.setId(categoryUpdate.getId());
        category.setKeywords(categoryUpdate.getKeywords());
        category.setDesc(categoryUpdate.getDesc());
        category.setIconUrl(categoryUpdate.getIconUrl());
        category.setPicUrl(categoryUpdate.getPicUrl());
        category.setLevel(categoryUpdate.getLevel());
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public CategoryCreateVo create(CategoryCreateBo categoryCreateBo) {
        Category category = new Category();
        category.setName(categoryCreateBo.getName());
        category.setKeywords(categoryCreateBo.getKeywords());
        category.setLevel(categoryCreateBo.getLevel());
        if("L1".equals(categoryCreateBo.getLevel())){
            //创建的是一级类目，和所有的类目判重
            if (judgeRepeat(categoryCreateBo)) return null;
            if ("".equals(categoryCreateBo.getPicUrl()) || "".equals(categoryCreateBo.getIconUrl())) {
                //不传照片创建失败
                CategoryCreateVo categoryCreateVo = new CategoryCreateVo();
                categoryCreateVo.setDesc("a");
                return categoryCreateVo;
            }
            category.setPid(0);
        }else{
            //创建的是二级类目，这个时候必须要有父类目，和所有的没被删除类目名称比较，判重
            if (judgeRepeat(categoryCreateBo)) return null;
            if ("".equals(categoryCreateBo.getPicUrl()) || "".equals(categoryCreateBo.getIconUrl()) || categoryCreateBo.getPid() == 0) {
                //不传照片，沒有父类目创建失败
                CategoryCreateVo categoryCreateVo = new CategoryCreateVo();
                categoryCreateVo.setDesc("b");
                return categoryCreateVo;
            }
            category.setPid(categoryCreateBo.getPid());
        }
        category.setDesc(categoryCreateBo.getDesc());
        category.setIconUrl(categoryCreateBo.getIconUrl());
        category.setPicUrl(categoryCreateBo.getPicUrl());
        Byte sortOrder = 50;
        category.setSortOrder(sortOrder);
        category.setDeleted(false);
        Date addTime = new Date();
        category.setAddTime(addTime);
        category.setUpdateTime(addTime);
        categoryMapper.insertSelective(category);
        Integer id = category.getId();
        CategoryCreateVo categoryCreateVo = new CategoryCreateVo();
        categoryCreateVo.setId(id);
        categoryCreateVo.setName(categoryCreateBo.getName());
        categoryCreateVo.setKeywords(categoryCreateBo.getKeywords());
        categoryCreateVo.setDesc(categoryCreateBo.getDesc());
        categoryCreateVo.setPid(categoryCreateBo.getPid());
        categoryCreateVo.setIconUrl(categoryCreateBo.getIconUrl());
        categoryCreateVo.setPicUrl(categoryCreateBo.getPicUrl());
        categoryCreateVo.setLevel(categoryCreateBo.getLevel());
        categoryCreateVo.setAddTime(addTime);
        categoryCreateVo.setUpdateTime(addTime);
        return categoryCreateVo;
    }

    @Override
    public CategoryIndexVO index() {
        int catlogListLimit = configMapper.getCatlogList("market_wx_catlog_list");
        int subCurrentCategoryLimit = configMapper.getCatlogList("market_wx_catlog_goods");
        //第一次打开分类页面，永远以第一个分类为当前页面，这里不用传入id，直接在SQL判断
        Category currentCategory = categoryMapper.selectCurrentCategory(null);
        Integer currentCategoryId = currentCategory.getId();
        List<Category> categoryList = categoryMapper.selectCategoryList("L1",catlogListLimit,null);
         List<Category> subCurrentCategory = categoryMapper.selectCategoryList("L2",subCurrentCategoryLimit,currentCategoryId);
        CategoryIndexVO categoryIndexVO = new CategoryIndexVO(currentCategory,categoryList,subCurrentCategory);
        return categoryIndexVO;
    }

    @Override
    public CategoryIndexVO current(int id){

        int subCurrentCategoryLimit = configMapper.getCatlogList("market_wx_catlog_goods");
        Category currentCategory = categoryMapper.selectCurrentCategory(id);
        Integer currentCategoryId = currentCategory.getId();
        List<Category> subCurrentCategory = categoryMapper.selectCategoryList("L2",subCurrentCategoryLimit,currentCategoryId);
        CategoryIndexVO categoryIndexVO = new CategoryIndexVO(currentCategory,subCurrentCategory);
        return categoryIndexVO;
    }

    private boolean judgeRepeat(CategoryCreateBo categoryCreateBo) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category1 : categories) {
            if (categoryCreateBo.getName().equals(category1.getName())) {
                return true;
            }
        }
        return false;
    }
}
